package com.tenement.controller.util;

import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.tenement.base.constants.SysmConstants.state;
import com.tenement.base.exception.ParameterException;
//import com.tenement.utils.phonemsg.SendPhoneVerifyCodeUtil;
import com.tenement.mybatis.entity.User;
import com.tenement.service.interfaces.IUserService;
import com.tenement.utils.base.ProduceUtil;
import com.tenement.utils.phonemsg.SendPhoneVerifyCodeUtil;

/**
 * 手机验证码
 * 
 * @author wk
 *
 */
@SuppressWarnings("unused")
@Controller
public class PhoneMsgController {

	private static final Log logger = LogFactory.getLog(PhoneMsgController.class);
	@Autowired
	private IUserService iUserService;

	// 手机验证码
	String phoneCode;

	// 获取验证码时，将发送验证码的手机号存入到Map中，在提交数据时和Map中的手机号进行对比。
	Map<String, String> phoneMap = new HashMap<String, String>();

	/**
	 * 借助网络发送验证码,验证码存在session中
	 * 
	 * @param phoneNumber
	 * @param request
	 * @return
	 */
	public String getPhoneCodeNet(String phoneNumber, HttpServletRequest request) {
		// 发送手机验证码工具类，调用阿里云短信服务,判断调用阿里短信服务状态
		String status = SendPhoneVerifyCodeUtil.returnCode(request, phoneNumber);
		if (status.equals(state.Success)) {
			phoneMap.put("phoneNumber", phoneNumber);// 将手机号存到Map中
			return state.Success;
		} else {
			return state.Failed;
		}
	}

	/**
	 * 本地生成
	 * 
	 * @param phoneNumber
	 * @param request
	 */
	public String getPhoneCodeLocal(String phoneNumber, HttpServletRequest request) {
		// phoneCode = ProduceUtil.productCode();// 生成六位验证码
		phoneCode = "666666";
		logger.info("【生成的手机验证码】" + phoneCode);
		if (phoneCode != null || "".equals(phoneCode)) {
			phoneMap.put("phoneNumber", phoneNumber);// 将手机号存到Map中
			phoneMap.put("phoneCode", phoneCode);// 保存手机验证码数据提交时进行校验
			return state.Success;
		} else {
			return state.Failed;
		}
	}

	/**
	 * 发送手机验证码
	 * 
	 * @param phoneNumber
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/sendPhoneCode.action")
	public @ResponseBody String sendPhoneCode(@RequestBody String phoneNumber, HttpServletRequest request) {

		try {
			if (phoneNumber == null || "".equals(phoneNumber)) {
				throw new Exception("【-传入的手机号为空-】");
			}
			return getPhoneCodeLocal(phoneNumber, request);
			// return getPhoneCodeNet(phoneNumber, request);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return state.Failed;
	}

	/**
	 * 校验用户输入的手机验证码,在身份验证时需要此方法(不需判断手机号是否一致)
	 * 
	 * @param code
	 * @return
	 */
	@RequestMapping(value = "/verifyPhoneCode.action")
	public @ResponseBody String verifyPhoneCode(@RequestBody String data, HttpSession session) {
		JSONObject jsonstr = JSON.parseObject(data);
		String code = jsonstr.getString("code");
		String phoneNumber = jsonstr.getString("phoneNumber");
		try {
			if (code == null || "".equals(code)) {
				throw new ParameterException("【-用户输入的手机验证码为空-】");
			} else if (phoneNumber == null || "".equals(phoneNumber)) {
				throw new ParameterException("【-用户输入的手机号为空-】");
			} else {
				// ①网络获取存入session中的phoneCode
				// phoneCode = (String) session.getAttribute("phoneCode");
				// ②本地获取，直接赋值
				if (phoneCode != null) {
					if (phoneCode.equals(code)) {
						return state.Success;// 用户输入的手机验证码正确1
					} else {
						return state.Failed;// 用户输入的手机验证码错误0
					}
				} else {
					return state.Failed;
				}
			}
		} catch (ParameterException e) {
			e.printStackTrace();
		}
		return state.Failed;
	}

	/**
	 * 校验用户输入的手机验证码,在身份验证时需要此方法(需判断手机号是否一致)
	 * 
	 * @param data
	 * @return
	 */
	@RequestMapping("/verifyPhoneCode2.action")
	public @ResponseBody String verifyPhoneCode2(@RequestBody String data) {
		JSONObject jsonstr = JSON.parseObject(data);
		String u_id = jsonstr.getString("u_id"); // 用户ID
		String u_telephone = jsonstr.getString("u_telephone");// 用户修改的手机号
		String phoneCode = jsonstr.getString("phoneCode");// 手机验证码
		try {
			if (u_id == null || "".equals(u_id)) {
				throw new Exception("【用户uId为空】");
			}
			if (u_telephone == null || "".equals(u_telephone)) {
				throw new Exception("【用户u_telephone为空】");
			}
			if (phoneCode == null || "".equals(phoneCode)) {
				throw new Exception("【用户phoneCode为空】");
			}
		} catch (Exception e) {
			logger.error("【修改用户绑定的手机号参数为空】");
		}
		if (!u_telephone.equals(phoneMap.get("phoneNumber"))) {
			return "-1";// 提示用户重新获取验证码
		} else if (!phoneCode.equals(phoneMap.get("phoneCode"))) {
			return "-2";// 用户输入的验证码错误
		} else {
			return state.Success;// 修改成功,1
		}
	}

	/**
	 * 修改用户绑定的手机号
	 * 
	 * @param data
	 * @return
	 */
	@RequestMapping("/modifyPhoneNumber.action")
	public @ResponseBody String modifyPhoneNumber(@RequestBody String data, HttpServletRequest request) {
		JSONObject jsonstr = JSON.parseObject(data);
		String u_id = jsonstr.getString("u_id"); // 用户ID
		String u_telephone = jsonstr.getString("u_telephone");// 用户修改的手机号
		String phoneCode = jsonstr.getString("phoneCode");// 手机验证码
		User user = new User();
		try {
			if (u_id == null || "".equals(u_id)) {
				throw new ParameterException("【用户uId为空】");
			}
			if (u_telephone == null || "".equals(u_telephone)) {
				throw new ParameterException("【用户u_telephone为空】");
			}
			if (phoneCode == null || "".equals(phoneCode)) {
				throw new ParameterException("【用户phoneCode为空】");
			}
		} catch (ParameterException e) {
			e.printStackTrace();
		}
		if (!u_telephone.equals(phoneMap.get("phoneNumber"))) {
			return "-1";// 提示用户重新获取验证码
		} else if (!phoneCode.equals(phoneMap.get("phoneCode"))) {
			return "-2";// 用户输入的验证码错误
		} else {
			user.setuId(Integer.parseInt(u_id));
			user.setuTelephone(u_telephone);
			iUserService.modifyUserPhone(user, request);
			return state.Success;// 修改成功,1
		}
	}

	/**
	 * 预约租房，用户在房屋详情页面，点击我要租房，将用户预约的房屋信息发送给房主 您发布的房源被：【18772306730】预约
	 * 
	 * @return
	 */
	@RequestMapping(value = "/sendReserveMsg")
	public @ResponseBody String sendPhoneCode() {
		return null;
	}
}
